package com.wkr.demo;

import com.mysql.cj.MysqlType;

import java.io.*;
import java.sql.*;
import java.util.Properties;

public class Link {

    static String URL = "jdbc.url";
    static String DRIVERCLASS = "jdbc.driverClass";
    static String USERNAME = "jdbc.username";
    static String PASSWORD = "jdbc.password";

    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
        getDB();
    }

    public static void getDB() throws ClassNotFoundException, SQLException, IOException {
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        String filePath = Link.class.getResource("/").getPath() + File.separator + "jdbc.properties";
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            // 使用properties对象加载输入流
            properties.load(in);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                in.close();
            }
        }
        //获取key对应的value值

        String url = properties.getProperty(URL);
        String user = properties.getProperty(USERNAME);
        String password = properties.getProperty(PASSWORD);
        //1.加载驱动程序
        Class.forName(properties.getProperty(DRIVERCLASS));
        //2.获得数据库链接
        Connection conn = DriverManager.getConnection(url, user, password);
        String sql = "CALL idon(?, ?)";
        CallableStatement statement = conn.prepareCall(sql);
        statement.setInt(1, 100);
        statement.registerOutParameter(2, Types.VARCHAR);
        statement.execute();
        String name = statement.getString(2);
        System.out.println(name);
        String sql_FindPeople = "SELECT t.table_name FROM information_schema.TABLES t WHERE t.TABLE_NAME ='people'";
//        ResultSet resultSet = statement.executeQuery(sql_FindPeople);
//        if (resultSet.next()) {
//            String sql_insert_info = "INSERT people(pid, pname, page) VALUES(1, 'hello', 23)";
//            statement.execute(sql_insert_info);
//        } else {
//            String sql_create_table = "CREATE TABLE people (pid int PRIMARY KEY, pname varchar(20), page int)";
//            statement.execute(sql_create_table);
//        }

//        ResultSet resultSet = statement.executeQuery(sql);
//        if (resultSet.next()) {
//            System.out.println(resultSet.getFetchSize());
//        }
        statement.close();
        conn.close();
    }
}
